/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



public class BinaryTree<E> {
    
    //Variables
    protected E value;                      // valor del nodo
    protected BinaryTree<E> parent;         // padre del nodo
    protected BinaryTree<E> left, right;    //hijos del nodo
    
    
    // post: constructor that generates an empty node
    public BinaryTree(){
        value = null;
        parent = left = right = null;
    }
    
    // post: returns a tree referencing value and two empty subtrees
    public BinaryTree(E value){
        this.value = value;
        right = left = new BinaryTree<E>();
        left=null;
        right=null;
    }

    // post: returns a tree referencing value and two subtrees
        public BinaryTree(E value, BinaryTree<E> left, BinaryTree<E> right){
        this.value = value;
        if (left == null)
            left = new BinaryTree<E>();
        setLeft(left);
        if (right == null)
            left = new BinaryTree<E>();
        setRight(right);
    }
    
    
    // post: sets left subtree to newLeft
    // re-parents newLeft if not null
    public void setLeft(BinaryTree<E> newLeft){
        if (left != null && left.getParent() == this)
            left.setParent(null);
        left = newLeft;
        left.setParent(this);
    }
    
    // post: sets left subtree to newLeft
    // re-parents newLeft if not null
    public void setRight(BinaryTree<E> newRight){
        if (right != null && right.getParent() == this)
            right.setParent(null);
        right = newRight;
        right.setParent(this);
    }
    
    // post: re-parents this node to parent reference, or null
    protected void setParent(BinaryTree<E> newParent){
            parent = newParent;
    }   
    
    // post: returns reference to (possibly empty) left subtree
    public BinaryTree<E> getLeft(){
        return left;
    }
    
    // post: returns reference to (possibly empty) left subtree
    public BinaryTree<E> getRight(){
        return right;
    }
    
    // post: returns reference to parent node, or null
    public BinaryTree<E> getParent(){
        return parent;
    }     
    
    // post: returns value associated with this node 
    public E getValue(){
        return value;
    }
    
    // post: sets the value associated with this node
    public void setValue(E value){
        this.value = value;
    }
  
}